package com.vitabuybuy.tv.ui.mine.bidding

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import com.google.android.material.tabs.TabLayout
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityMyBiddingBinding
import com.vitabuybuy.tv.dto.MyBiddingListDto
import com.vitabuybuy.tv.ui.home.bidd.BiddingCommodityActivity
import com.vitabuybuy.tv.ui.mine.bidding.viewmodel.MyBiddingViewModel
import com.vitabuybuy.tv.ui.mine.order.OrderListActivity
import kotlinx.android.synthetic.main.activity_my_bidding.*

class MyBiddingActivity :
    BaseActivity<MyBiddingViewModel, ActivityMyBiddingBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_my_bidding

    private var status = "待开始"
    private var pageNumber = 1

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewModel = viewModel
        setTitle(getString(R.string.my_bidding))
        mTvRight.apply {
            text = getString(R.string.has_get)
            visibility = View.VISIBLE
            setTextColor(ContextCompat.getColor(mContext, R.color.mainColor))
            setOnClickListener {
                OrderListActivity.openActivity(mContext, OrderListActivity.WAITING_PAY)
                finish()
            }
        }

        val list = mutableListOf(
            getString(R.string.wait_bidding),
            getString(R.string.is_bidding),
            getString(R.string.has_over)
        )
        tabLayout.apply {
            for (str in list) {
                addTab(tabLayout.newTab().setText(str))
            }
        }
    }

    override fun observeData() {
        viewModel.getGoodsList(status, pageNumber)
    }
    override fun eventHandling() {
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                status = when (tab?.text) {
                    getString(R.string.wait_bidding) -> getString(R.string.wait_start)
                    getString(R.string.is_bidding) -> getString(R.string.underway)
                    getString(R.string.has_over) -> getString(R.string.has_over)
//                    getString(R.string.has_get)->"已拍下"
                    else -> ""
                }
                pageNumber = 1
                viewModel.getGoodsList(status, pageNumber)
            }

            override fun onTabReselected(p0: TabLayout.Tab?) {
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }
        })
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getGoodsList(status, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getGoodsList(status, pageNumber)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getGoodsList(status, pageNumber)
            }
            1 -> refresh.onLoad(msg.arg1)
            2 -> {
                BiddingCommodityActivity.openActivity(mContext,(msg.obj as MyBiddingListDto).product.id,(msg.obj as MyBiddingListDto).id)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, MyBiddingActivity::class.java)
        }
    }

}
